<?php

namespace app\controllers\admin;

use yii;
use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\admin\IvtcodeService;
use yii\web\Controller;

class IvtcodeController extends Controller
{
    public $req = array();
    public $user = array();

    /**
     * 安全 校验 屏蔽
     * 预处理  权限等等操作处理
     */
    public function beforeAction($action){
        $this->req = Helper::in(array_merge($_POST, $_GET));
        $this->user = Helper::checksign($this->req);
        unset($this->req['token']);
        return true;
    }

    /**
     * 列表
     *
     */
    public function actionGetList(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'num'   => 'integer',
            'page'  => 'integer',
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = IvtcodeService::getInstance()->getCodeList($req);
        Helper::right_out($req,$res);
    }

    /**
     * 生成单个二维码
     */
    public function actionCreateSingleCode(){
        $req = $this->req;
        $res = IvtcodeService::getInstance()->createSingleCode();
        Helper::right_out($req,$res);
    }


    /**
     * 保存二维码的设置
     */
    public function actionSaveCodeSetting(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'code'   => "required|alpha_num",
            'level'  => "required|in:1,2,3",
            'expire' => "required|date",
            'roomID' => "required|integer",
            'row'    => "required|integer|max:13|min:1",
            'col'    => "required|integer|max:21|min:1",
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = IvtcodeService::getInstance()->saveCodeSetting($req);
        Helper::right_out($req,$res);
    }

    /**
     * 批量生成邀请码
     */
    public function actionCreateBatchCode(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'level'  => "required|in:1,2,3",
            'count'  => "required|integer|max:250|min:1",
            'expire' => "required|date",
            'roomID' => "required|integer",
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = IvtcodeService::getInstance()->createBatchCode($req);
        Helper::right_out($req,$res);
    }


    /**
     * 导出邀请码
     */
    public function actionExportCodeList(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'ID' => "required|string",
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = IvtcodeService::getInstance()->exportCodeList($req['ID']);
        Helper::right_out($req,$res);
    }


    /**
     * 删除邀请码 （过期才可删除）
     */
    public function actionDelCode(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'ID' => "required|integer",
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = IvtcodeService::getInstance()->delCode($req['ID']);
        Helper::right_out($req,$res);
    }


}
